package com.example.Servlet;



import com.example.entity.User;
import com.example.service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;


@WebServlet(name = "Lo ginServlet", value = "/LoginServlet")
public class Loginservlet extends HttpServlet {
    UserService  userService= new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
         String username = request.getParameter("username");
         System.out.println(username);
         if(("admin").equals(username)){
             PrintWriter writer =response.getWriter();
              writer.println("a.jpeg");
              writer.flush();
         }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username.equals("admin") && password.equals("123")) {
            //成功！-》成功页面
            //转发，将请求转发到ok.jsp地址 forward（请求相应对象继续跟随）
            List<User> userList = null;
            try {
                userList = userService.getAll();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            request.setAttribute("userList", userList);

            request.getRequestDispatcher("ok.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("error.jsp").forward(request, response);
        }
    }
}